local BaseData = require("data.base.BaseData")
local MagicKeyData = class("MagicKeyData", BaseData)
local MagicKeyUnitData = require("data.MagicKeyUnitData")
local StoreConst = require("const.StoreConst")

function MagicKeyData:ctor()
    MagicKeyData.super.ctor(self)
    self._mkList = {}
end

function MagicKeyData:init()
    self:_initData()
end

function MagicKeyData:_initData()
    self._globalMagickeyId = G_StoreManager:getGlobalId(StoreConst.MAGICKEY_GLOBALID) or 0
    local list = G_StoreManager:getDataList(StoreConst.MAGICKEY)
    for i = 1, #list do
        self:_setMagicUnitData(list[i])
    end
end

function MagicKeyData:save()
    G_StoreManager:setGlobalId(StoreConst.MAGICKEY_GLOBALID, self._globalMagickeyId)
    local result = {}
    for _, unit in pairs(self._mkList) do
        table.insert(result, unit:getPureParam())
    end
    G_StoreManager:setDataList(StoreConst.MAGICKEY, result)
end

function MagicKeyData:_setMagicUnitData(data)
    local unit = self._mkList["k_" .. data.id]
    if not unit then
        unit = MagicKeyUnitData.new()
        self._mkList["k_" .. data.id] = unit
    end
    unit:update(data)
end

function MagicKeyData:getUnitById(id)
    return self._mkList["k_" .. id]
end

function MagicKeyData:insertData(datas)
    for i = 1, #datas do
        self:_setMagicUnitData(datas[i])
    end
end

function MagicKeyData:updateData(datas)
    for i = 1, #datas do
        self:_setMagicUnitData(datas[i])
    end
end

function MagicKeyData:deleteData(ids)
    for i = 1, #ids do
        self._mkList["k_" .. ids[i]] = nil
    end
end

function MagicKeyData:getList()
    local result = {}
    for _, unit in pairs(self._mkList) do
        table.insert(result, unit)
    end
    return result
end

function MagicKeyData:addData(base_id)
    local data = {}
    printDebug(" MagicKeyData:addData " .. base_id)
    self._globalMagickeyId = self._globalMagickeyId + 1
    data.id = self._globalMagickeyId
    data.base_id = base_id
    data.level = 1
    self:_setMagicUnitData(data)
end

function MagicKeyData:equipMagickeyToHero(magicId, heroId)
    if magicId > 0 and heroId > 0 then
        local heroUnit = G_UserData:getHero():getUnitById(heroId)
        local unit = self._mkList["k_" .. magicId]
        local oldMagicId = heroUnit:getMagickeyId()
        if oldMagicId > 0 then
            local oldUnit = self._mkList["k_" .. oldMagicId]
            oldUnit:setHeroId(0)
        end
        local oldHeroId = unit:getHeroId()
        if oldHeroId > 0 then
            local oldHeroUnit = G_UserData:getHero():getUnitById(oldHeroId)
            oldHeroUnit:setMagickeyId(0)
        end
        heroUnit:setMagickeyId(magicId)
        unit:setHeroId(heroId)
        G_SignalManager:dispatch(SignalConst.EVENT_MAGICKEY_EQUIP_SUCCESS, magicId)
    end
end

function MagicKeyData:clearMagickeyInHero(magicId)
    if magicId > 0 then
        local unit = self._mkList["k_" .. magicId]
        local heroId = unit:getHeroId()
        local heroUnit = G_UserData:getHero():getUnitById(heroId)
        if heroUnit then
            heroUnit:setMagickeyId(0)
        end
        unit:setHeroId(0)
    end
    G_SignalManager:dispatch(SignalConst.EVENT_MAGICKEY_CLEAR_SUCCESS, magicId)
end

function MagicKeyData:upgradeMagickey(magicId)
    local ErrorCodeConst = require("const.ErrorCodeConst")
    local MagicKeyDataHelper = require("data.helper.MagicKeyDataHelper")
    local UserDataHelper = require("data.helper.UserDataHelper")
    local unit = self._mkList["k_" .. magicId]
    local config = unit:getConfig()
    local curLevel = unit:getLevel()

    if curLevel >= config.lv_max then
        return ErrorCodeConst.ERROR_LEVEL_MAX
    end
    local cost = MagicKeyDataHelper.getMagicKeyUpCost(unit)
    local haveNum = UserDataHelper.getNumByTypeValue(cost.cost_type1, cost.cost_value1)
    if haveNum >= cost.cost_size1 then
        UserDataHelper.useSomeThing(cost.cost_type1, cost.cost_value1, cost.cost_size1)
        unit:setLevel(curLevel + 1)
        G_SignalManager:dispatch(SignalConst.EVENT_MAGICKEY_UPGRADE_SUCCESS, magicId)
        return ErrorCodeConst.OK
    else
        return ErrorCodeConst.ERROR_COST_NOT
    end
end

return MagicKeyData
